﻿import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.15
import QtQuick.Window 2.15

import "." // 导入项目的根目录

Window {
    id: pciePage
    visible: true
    width: 1200
    height: 768
    title: "pcie Test"

    // 这个函数是模拟获取pcie状态的函数，实际应用中需要替换为真实的获取状态逻辑
    function getpcieStatus() {
        // 模拟pcie状态数据，这里只是随机生成一个字符串作为示例
        var status = "数据更新" + Math.floor(Math.random() * 100);
        return status;
    }
    // 定时器，模拟pcie数据更新
    Timer {
        id: pcieTimer
        interval: 1000 // 更新间隔，单位为毫秒
        repeat: true   // 是否重复执行
        onTriggered: {
            // 这里是模拟pcie数据更新的地方
            // 假设我们有一个函数getpcieStatus()来获取pcie的状态
            var pcieStatus = getpcieStatus();

            // 更新文本显示框的内容
            //pcieDataDisplay.text = "pcie状态: " + pcieStatus;
        }
    }

    Item {
        id: pcieItem
        anchors.fill : parent

        ColumnLayout {
            id: pcieColumn
             anchors.left : pcieItem.left
             height : pcieItem.height
             width : pcieItem.width * 0.90

            // 上半部分区域
            Rectangle {
                id:pcieTop
                width: pcieColumn.width * 0.99
                height: pcieColumn.height * 0.60
                anchors.top: pcieColumn.top
                anchors.topMargin: 3
                anchors.left: parent.left
                anchors.leftMargin: 3
                border.color: "#c0c0c0"     // 边框颜色
                border.width: 2             // 边框宽度

                color: "#f0f0f0"

                // 上半部分第一个框
                Rectangle {
                    id: pcieSetup
                    width: pcieTop.width * 0.99
                    height: pcieTop.height * 0.49
                    anchors.top: pcieTop.top
                    anchors.topMargin: 6
                    anchors.left: pcieTop.left
                    anchors.leftMargin: 6

                    ColumnLayout {
                        anchors.centerIn: parent
                        spacing: 10

                        RowLayout {
                            id: pcieTestGroup0
                            //anchors.fill: parent
                            width: pcieTop.width * 0.99
                            height: pcieTop.height * 0.10
                            anchors.top: pcieTop.top
                            anchors.topMargin: 5
                            anchors.left: pcieTop.left
                            anchors.leftMargin: 5
                            spacing: 15

                            Text {
                                id: pcieTestGroup0Text
                                width: pcieTop.width * 0.2
                                anchors.left: pcieTop.left
                                anchors.leftMargin: 5
                                text: "PCIe端口0:"
                            }

                            TextField {
                                id: pcieTestGroup0Input
                                width: pcieTop.width * 0.20
                                height: pcieSetup.height * 0.15
                                placeholderText: "pcie test"
                            }

                            Text {
                                id: pcieTestGroup0Interface
                                width: pcieTop.width * 0.2
                                anchors.left: pcieTop.left
                                anchors.leftMargin: 5
                                text: "测试参数:"
                            }

                            TextField {
                                id: pcieTestGroup0InterfaceInput
                                width: pcieTop.width * 0.20
                                height: pcieSetup.height * 0.15
                                placeholderText: "-port3303"
                            }

                            Button {
                                id: clearDatabutton0
                                text: "测 试"
                                width: pcieTop.width * 0.20
                                height: pcieSetup.height * 0.15
                                onClicked: {
                                    var host = backend.host;
                                    var user = backend.user;
                                    var command = "ls -l";
                                    pcietest.sendSshCommand(host, user, command);
                                    console.log("pcie 测试 接口 0");
                                    pcieTimer.start();
                                }
                            }
                        }

                        RowLayout {
                            id: pcieTestGroup1
                            width: pcieTop.width * 1
                            height: pcieTop.height * 0.1
                            anchors.top: pcieTestGroup0.bottom
                            anchors.topMargin: 5
                            anchors.left: pcieTop.left
                            anchors.leftMargin: 5
                            spacing: 15

                            Text {
                                id: pcieTestGroup1Text
                                width: pcieTop.width * 0.2
                                anchors.left: pcieTop.left
                                anchors.leftMargin: 5
                                text: "PCIe端口1:"
                            }

                            TextField {
                                id: pcieTestGroup1Input
                                width: pcieTop.width * 0.25
                                placeholderText: "pcie test"
                            }

                            Text {
                                id: pcieTestGroup1Interface
                                width: pcieTop.width * 0.2
                                anchors.left: pcieTop.left
                                anchors.leftMargin: 5
                                text: "测试参数:"
                            }

                            TextField {
                                id: pcieTestGroup1InterfaceInput
                                width: pcieTop.width * 0.20
                                height: pcieSetup.height * 0.15
                                placeholderText: "/data/ZhangDong/DAS1/APP/cpp/PrintTime/build/PrintTime"
                            }

                            Button {
                                id: clearDatabutton1
                                text: "测 试"
                                width: pcieTop.width * 0.20
                                height: pcieSetup.height * 0.15
                                onClicked: {
                                    var host = backend.host;
                                    var user = backend.user;
                                    //var command ="/data/ZhangDong/DAS1/APP/cpp/PrintTime/build/PrintTime";
                                    pcietest.sendRemoteCommand(host, user, pcieTestGroup1InterfaceInput.text);
                                    console.log("pcie 测试 接口 1")
                                }
                            }
                        }

                        RowLayout {
                            id: pcieTestGroup2
                            width: pcieTop.width * 1
                            height: pcieTop.height * 0.1
                            anchors.top: pcieTestGroup1.bottom
                            anchors.topMargin: 5
                            anchors.left: pcieTop.left
                            anchors.leftMargin: 5
                            spacing: 15

                            Text {
                                id: pcieTestGroup2Text
                                width: pcieTop.width * 0.2
                                anchors.left: pcieTop.left
                                anchors.leftMargin: 5
                                text: "PCIe端口2:"
                            }

                            TextField {
                                id: pcieTestGroup2Input
                                width: pcieTop.width * 0.25
                                placeholderText: "pcie test"
                            }

                            Text {
                                id: pcieTestGroup2Interface
                                width: pcieTop.width * 0.2
                                anchors.left: pcieTop.left
                                anchors.leftMargin: 5
                                text: "测试参数:"
                            }

                            TextField {
                                id: pcieTestGroup2frentInput
                                width: pcieTop.width * 0.20
                                height: pcieSetup.height * 0.15
                                placeholderText: "/data/ZhangDong/DAS1/APP/cpp/PrintTime/build/PrintTime"
                            }

                            Button {
                                id: clearDatabutton2
                                text: "测 试"
                                width: pcieTop.width * 0.20
                                height: pcieSetup.height * 0.15
                                onClicked: {
                                    var host = backend.host;
                                    var user = backend.user;
                                    var command ="/data/ZhangDong/DAS1/APP/cpp/PrintTime/build/PrintTime";
                                   pcietest.sendRemoteCommand(host, user, pcieTestGroup2InterfaceInput.text);
                                    console.log("pcie 测试 接口 2")
                                }
                            }
                        }

                        RowLayout {
                            id: pcieTestGroup3
                            width: pcieTop.width * 1
                            height: pcieTop.height * 0.1
                            anchors.top: pcieTestGroup2.bottom
                            anchors.topMargin: 5
                            anchors.left: pcieTop.left
                            anchors.leftMargin: 5
                            spacing: 15

                            Text {
                                id: pcieTestGroup3Text
                                width: pcieTop.width * 0.2
                                anchors.left: pcieTop.left
                                anchors.leftMargin: 5
                                text: "PCIe端口3:"
                            }

                            TextField {
                                id: pcieTestGroup3Input
                                width: pcieTop.width * 0.25
                                placeholderText: "pcie test"
                            }

                            Text {
                                id: pcieTestGroup3Interface
                                width: pcieTop.width * 0.2
                                anchors.left: pcieTop.left
                                anchors.leftMargin: 5
                                text: "测试参数:"
                            }

                            TextField {
                                id: pcieTestGroup3InterfaceInput
                                width: pcieTop.width * 0.20
                                height: pcieSetup.height * 0.15
                                placeholderText: "/data/ZhangDong/DAS1/APP/cpp/PrintTime/build/PrintTime"
                            }

                            Button {
                                id: clearDatabutton3
                                text: "测 试"
                                width: pcieTop.width * 0.20
                                height: pcieSetup.height * 0.15
                                onClicked: {
                                    var host = backend.host;
                                    var user = backend.user;
                                    var command ="/data/ZhangDong/DAS1/APP/cpp/PrintTime/build/PrintTime";
                                   pcietest.sendRemoteCommand(host, user, pcieTestGroup3InterfaceInput.text);
                                    console.log("pcie 测试 接口 3")
                                }
                            }
                        }
                    }
                }

                // 上半部分第二个框
                Rectangle {
                    id: pcieTopStatus
                    width: pcieTop.width * 0.99
                    height: pcieTop.height * 0.48
                    anchors.top: pcieSetup.bottom
                    anchors.topMargin: 4
                    anchors.left: pcieTop.left
                    anchors.leftMargin: 6

                    ColumnLayout {
                        anchors.centerIn: parent
                        spacing: 10

                        RowLayout {
                            id: pcieTestGroup4
                            //anchors.fill: parent
                            width: pcieTop.width * 0.99
                            height: pcieTop.height * 0.10
                            anchors.top: pcieTopStatus.top
                            anchors.topMargin: 5
                            anchors.left: pcieTop.left
                            anchors.leftMargin: 5
                            spacing: 15

                            Text {
                                id: pcieTestGroup4Text
                                width: pcieTop.width * 0.2
                                anchors.left: pcieTop.left
                                anchors.leftMargin: 5
                                text: "PCIe端口4:"
                            }

                            TextField {
                                id: pcieTestGroup4Input
                                width: pcieTop.width * 0.25
                                height: pcieSetup.height * 0.15
                                placeholderText: "pcie test"
                            }

                            Text {
                                id: pcieTestGroup4Interface
                                width: pcieTop.width * 0.2
                                anchors.left: pcieTop.left
                                anchors.leftMargin: 5
                                text: "测试参数:"
                            }

                            TextField {
                                id: pcieTestGroup4InterfaceInput
                                width: pcieTop.width * 0.20
                                height: pcieSetup.height * 0.15
                                placeholderText: "/data/ZhangDong/DAS1/APP/cpp/PrintTime/build/PrintTime"
                            }

                            Button {
                                id: clearDatabutton4
                                text: "测 试"
                                width: pcieTop.width * 0.20
                                height: pcieSetup.height * 0.15
                                onClicked: {
                                    var host = backend.host;
                                    var user = backend.user;
                                    var command ="/data/ZhangDong/DAS1/APP/cpp/PrintTime/build/PrintTime";
                                   pcietest.sendRemoteCommand(host, user, pcieTestGroup4InterfaceInput.text);
                                    console.log("pcie 测试 接口 4")
                                }
                            }
                        }

                        RowLayout {
                            id: pcieTestGroup5
                            //anchors.fill: parent
                            width: pcieTop.width * 0.99
                            height: pcieTop.height * 0.10
                            anchors.top: pcieTopStatus.top
                            anchors.topMargin: 5
                            anchors.left: pcieTop.left
                            anchors.leftMargin: 5
                            spacing: 15

                            Text {
                                id: pcieTestGroup5Text
                                width: pcieTop.width * 0.2
                                anchors.left: pcieTop.left
                                anchors.leftMargin: 5
                                text: "PCIe端口5:"
                            }

                            TextField {
                                id: pcieTestGroup5Input
                                width: pcieTop.width * 0.25
                                height: pcieSetup.height * 0.15
                                placeholderText: "pcie test"
                            }

                            Text {
                                id: pcieTestGroup5Interface
                                width: pcieTop.width * 0.2
                                anchors.left: pcieTop.left
                                anchors.leftMargin: 5
                                text: "测试参数:"
                            }

                            TextField {
                                id: pcieTestGroup5InterfaceInput
                                width: pcieTop.width * 0.20
                                height: pcieSetup.height * 0.15
                                placeholderText: "/data/ZhangDong/DAS1/APP/cpp/PrintTime/build/PrintTime"
                            }

                            Button {
                                id: clearDatabutton5
                                text: "测 试"
                                width: pcieTop.width * 0.20
                                height: pcieSetup.height * 0.15
                                onClicked: {
                                    var host = backend.host;
                                    var user = backend.user;
                                    var command ="/data/ZhangDong/DAS1/APP/cpp/PrintTime/build/PrintTime";
                                    ppcietest.sendRemoteCommand(host, user, pcieTestGroup5InterfaceInput.text);
                                    console.log("pcie 测试 接口 5")
                                }
                            }
                        }

                        RowLayout {
                            id: pcieTestGroup6
                            //anchors.fill: parent
                            width: pcieTop.width * 0.99
                            height: pcieTop.height * 0.10
                            anchors.top: pcieTopStatus.top
                            anchors.topMargin: 5
                            anchors.left: pcieTop.left
                            anchors.leftMargin: 5
                            spacing: 15

                            Text {
                                id: pcieTestGroup6Text
                                width: pcieTop.width * 0.2
                                anchors.left: pcieTop.left
                                anchors.leftMargin: 5
                                text: "PCIe端口6:"
                            }

                            TextField {
                                id: pcieTestGroup6Input
                                width: pcieTop.width * 0.25
                                height: pcieSetup.height * 0.15
                                placeholderText: "pcie test"
                            }

                            Text {
                                id: pcieTestGroup6Interface
                                width: pcieTop.width * 0.2
                                anchors.left: pcieTop.left
                                anchors.leftMargin: 5
                                text: "测试参数:"
                            }

                            TextField {
                                id: pcieTestGroup6InterfaceInput
                                width: pcieTop.width * 0.20
                                height: pcieSetup.height * 0.15
                                placeholderText: "/data/ZhangDong/DAS1/APP/cpp/PrintTime/build/PrintTime"
                            }

                            Button {
                                id: clearDatabutton6
                                text: "测 试"
                                width: pcieTop.width * 0.20
                                height: pcieSetup.height * 0.15
                                onClicked: {
                                    var host = backend.host;
                                    var user = backend.user;
                                    var command ="/data/ZhangDong/DAS1/APP/cpp/PrintTime/build/PrintTime";
                                    pcietest.sendRemoteCommand(host, user, pcieTestGroup6InterfaceInput.text);
                                    console.log("pcie 测试 接口 6")
                                }
                            }
                        }

                        RowLayout {
                            id: pcieTestGroup7
                            //anchors.fill: parent
                            width: pcieTop.width * 0.99
                            height: pcieTop.height * 0.10
                            anchors.top: pcieTopStatus.top
                            anchors.topMargin: 5
                            anchors.left: pcieTop.left
                            anchors.leftMargin: 5
                            spacing: 15

                            Text {
                                id: pcieTestGroup7Text
                                width: pcieTop.width * 0.2
                                anchors.left: pcieTop.left
                                anchors.leftMargin: 5
                                text: "PCIe端口7:"
                            }

                            TextField {
                                id: pcieTestGroup7Input
                                width: pcieTop.width * 0.25
                                height: pcieSetup.height * 0.15
                                placeholderText: "pcie test"
                            }

                            Text {
                                id: pcieTestGroup7Interface
                                width: pcieTop.width * 0.2
                                anchors.left: pcieTop.left
                                anchors.leftMargin: 5
                                text: "测试参数:"
                            }

                            TextField {
                                id: pcieTestGroup7InterfaceInput
                                width: pcieTop.width * 0.20
                                height: pcieSetup.height * 0.15
                               placeholderText: "/data/ZhangDong/DAS1/APP/cpp/PrintTime/build/PrintTime"
                            }

                            Button {
                                id: clearDatabutton7
                                text: "测 试"
                                width: pcieTop.width * 0.20
                                height: pcieSetup.height * 0.15
                                onClicked: {
                                    var host = backend.host;
                                    var user = backend.user;
                                    var command ="/data/ZhangDong/DAS1/APP/cpp/PrintTime/build/PrintTime";
                                    pcietest.sendRemoteCommand(host, user, pcieTestGroup7InterfaceInput.text);
                                    console.log("pcie 测试 接口 7")
                                }
                            }
                        }
                    }
                }
            }

            // 错误信息显示
            Text {
                id: pcieLogText
                color: "blue"
                anchors.top: pcieTop.bottom
                anchors.topMargin: 2
                anchors.left: pcieTop.left
                anchors.leftMargin: 10
                text: "输出日志:"
            }

            // 下半部分 显示数据
            Rectangle {
                id: pcieStatusDisplay
                width : pcieColumn.width  * 0.99
                height: pcieColumn.height * 0.35
                anchors.left: pcieTop.left
                anchors.top: pcieLogText.bottom
                anchors.topMargin: 4
                color: "#f0f0f0"
                border.color: "#c0c0c0"     // 边框颜色
                border.width: 2             // 边框宽度

                ScrollView{
                    id: scrollView
                    anchors.fill: parent
                    clip: true

                    // 文本显示框
                    TextArea {
                        id: pcieDataDisplay
                        anchors.fill: parent // 让 TextArea 填充整个 ScrollView
                        readOnly: true       // 使其只读，不可编辑
                        placeholderText: "接收到的数据"
                        color: "black"       // 设置字体颜色为黑色
                        background: Rectangle {
                            color: "transparent" // 背景透明，显示父元素的颜色
                        }
                        wrapMode: TextArea.Wrap // 自动换行
                        font.pixelSize: 14      // 字体大小（可以根据需要调整）
                    }
                }
            }

            // 该信号连接和注册方式，更加简单好用
            Connections {
                target: pcietest
                onProcessFinished: {
                    console.log("Process finished!")
                }
                onCommandOutput: {
                    // 获取当前时间，并格式化为日志记录的格式
                    var time = Qt.formatDateTime(new Date(), "yyyy-MM-dd HH:mm:ss") // 格式化为: "2024-12-18 11:22:30"

                    // 将时间和输出内容一起追加到文本框,append会自动让文本显示在最新位置
                    pcieDataDisplay.append("[" +time + "] " + output)
                }
            }

            // 错误信息显示
            Text {
                id: errorDisplay
                color: "red"
                anchors.top: pcieStatusDisplay.bottom
                anchors.topMargin: 2
                anchors.left: pcieTop.left
                anchors.leftMargin: 10
                text: "未连接......"
            }
        }

        Rectangle {
            id: pcieInfo
            anchors.right: pcieItem.right
            height       : pcieItem.height * 0.97
            width        : pcieItem.width * 0.09
            anchors.top: pcieItem.top
            anchors.topMargin: 3
            anchors.rightMargin: 10
            border.color: "#c0c0c0"     // 边框颜色
            border.width: 2             // 边框宽度

            Button {
                id: pcieExitButton
                text: "返 回"
                width: pcieInfo.width * 0.80
                height: pcieInfo.height * 0.05
                anchors.left: pcieInfo.left
                anchors.leftMargin: pcieInfo.width * 0.1
                anchors.top: pcieInfo.top
                anchors.topMargin: 5
                onClicked: {
                    console.log("返回主界面")
                    pciePage.visible = false
                    mainPage.visible = true
                }
            }

            Button {
                id: pcieClearButton
                text: "清 除"
                width: pcieInfo.width * 0.80
                height: pcieInfo.height * 0.05
                anchors.left: pcieInfo.left
                anchors.leftMargin: pcieInfo.width * 0.1
                anchors.top:pcieInfo.top
                anchors.topMargin: pcieInfo.height * 0.65
                onClicked: {
                    console.log("清除spi文本框信息")
                    pcieDataDisplay.clear()
                }
            }
        }
    }
}
